package b1.scenes.home
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.fight.*;
	import b1.scenes.run.*;
	import b1.utils.*;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.*;


	public class ActionPane extends Sprite
	{
		private var animalInfo:AnimalInfo;
		private const wNum:int = 2;
		private var itemNum:int = 0;
		private var msgBox:MessageBox;
		private var tip:BasicMessage;
		//index: 0, 跑路; 1, 回家; 2, 作案; 3, 逮捕; 4, 保释; 5, 驱逐
		private var actionItem0:BasicButton;
		private var actionItem1:BasicButton;
		private var actionItem2:BasicButton;
		private var actionItem3:BasicButton;
		private var actionItem4:BasicButton;
		private var actionItem5:BasicButton;
		
		public function ActionPane(animalInfo:AnimalInfo)
		{
			super();
			this.animalInfo = animalInfo;
			var timeLeft:int = animalInfo.timeToGetFree - ServerHelper.getInstance().now();
			//不在狱中
			if (timeLeft < 0)
			{
				//自己的动物
				if (animalInfo.ownerId == PlayerInfoHelper.getInstance().userId)
				{
					addActionItem(0);
					//自己的动物在别人家
					if (animalInfo.liveId != animalInfo.ownerId)
					{
						addActionItem(1);
					}
				}
				//别人的动物
				else 
				{
					addActionItem(2);
					//别人的动物在我家
					if (animalInfo.liveId == PlayerInfoHelper.getInstance().userId)
					{
						addActionItem(5);
					}
					//被通缉的动物
					if (animalInfo.wantedBones > 0 && animalInfo.timeToClose > ServerHelper.getInstance().now())
					{
						addActionItem(3);
					}
				}
			}
			//狱中的动物
			else
			{
				addActionItem(4);
			}
		}
		
		//index: 0, 跑路; 1, 回家; 2, 作案; 3, 逮捕; 4, 保释; 5, 驱逐
		public function addActionItem(index:int):void
		{
			var buttonText:String;
			var ActionButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr2");
			if (index == 0)
			{
				actionItem0 = new BasicButton(ActionButtonBgr, "跑路");
				actionItem0.x = (itemNum % wNum) * (actionItem0.width + 10);
				actionItem0.y = Math.floor(itemNum / wNum) * (actionItem0.height + 10);
				addChild(actionItem0);
				actionItem0.addEventListener(MouseEvent.CLICK, onRunAwayClick);
				actionItem0.addEventListener(MouseEvent.MOUSE_OVER, onRunAwayMouseOver);
				actionItem0.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			}
			else if (index == 1)
			{
				actionItem1 = new BasicButton(ActionButtonBgr, "回家");
				actionItem1.x = (itemNum % wNum) * (actionItem1.width + 10);
				actionItem1.y = Math.floor(itemNum / wNum) * (actionItem1.height + 10);
				addChild(actionItem1);
				actionItem1.addEventListener(MouseEvent.CLICK, onGoHomeClick);
				actionItem1.addEventListener(MouseEvent.MOUSE_OVER, onGoHomeMouseOver);
				actionItem1.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			}
			else if (index == 2)
			{
				if (this.animalInfo.id > 0)
				{
					actionItem2 = new BasicButton(ActionButtonBgr, "作案");
				}
				else
				{
					actionItem2 = new BasicButton(ActionButtonBgr, "战斗");
				}
				actionItem2.x = (itemNum % wNum) * (actionItem2.width + 10);
				actionItem2.y = Math.floor(itemNum / wNum) * (actionItem2.height + 10);
				addChild(actionItem2);
				actionItem2.addEventListener(MouseEvent.CLICK, onCrimeClick);
				actionItem2.addEventListener(MouseEvent.MOUSE_OVER, onCrimeMouseOver);
				actionItem2.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			}
			else if (index == 3)
			{
				actionItem3 = new BasicButton(ActionButtonBgr, "逮捕");
				actionItem3.x = (itemNum % wNum) * (actionItem3.width + 10);
				actionItem3.y = Math.floor(itemNum / wNum) * (actionItem3.height + 10);
				addChild(actionItem3);
				actionItem3.addEventListener(MouseEvent.CLICK, onArrestClick);
				actionItem3.addEventListener(MouseEvent.MOUSE_OVER, onArrestMouseOver);
				actionItem3.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			}
			else if (index == 4)
			{
				actionItem4 = new BasicButton(ActionButtonBgr, "保释");
				actionItem4.x = (itemNum % wNum) * (actionItem4.width + 10);
				actionItem4.y = Math.floor(itemNum / wNum) * (actionItem4.height + 10);
				addChild(actionItem4);
				actionItem4.addEventListener(MouseEvent.CLICK, onBailClick);
				actionItem4.addEventListener(MouseEvent.MOUSE_OVER, onBailMouseOver);
				actionItem4.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			}
			else if (index == 5)
			{
				actionItem5 = new BasicButton(ActionButtonBgr, "驱逐");
				actionItem5.x = (itemNum % wNum) * (actionItem5.width + 10);
				actionItem5.y = Math.floor(itemNum / wNum) * (actionItem5.height + 10);
				addChild(actionItem5);
				actionItem5.addEventListener(MouseEvent.CLICK, onExpelClick);
				actionItem5.addEventListener(MouseEvent.MOUSE_OVER, onExpelMouseOver);
				actionItem5.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			}
			itemNum++;
		}

		//跑路
		private function onRunAwayMouseOver(event:MouseEvent):void
		{
			var string:String = "跑到其他人的家中";
			tip = new BasicMessage(150, 25, string);
			tip.x = actionItem0.x + (actionItem0.width - tip.width) / 2;
			tip.y = actionItem0.y - tip.height;
			addChild(tip);
		}

		//回家
		private function onGoHomeMouseOver(event:MouseEvent):void
		{
			var string:String = "回到自己的家中";
			tip = new BasicMessage(130, 25, string);
			tip.x = actionItem1.x + (actionItem1.width - tip.width) / 2;
			tip.y = actionItem1.y - tip.height;
			addChild(tip);
		}

		//作案
		private function onCrimeMouseOver(event:MouseEvent):void
		{
			var string:String;
			if (this.animalInfo.id > 0)
			{
				 string = "向它作案";
			}
			else
			{
				 string = "开始战斗";
			}
			tip = new BasicMessage(90, 25, string);
			tip.x = actionItem2.x + (actionItem2.width - tip.width) / 2;
			tip.y = actionItem2.y - tip.height;
			addChild(tip);
		}

		//逮捕
		private function onArrestMouseOver(event:MouseEvent):void
		{
			var string:String = "逮捕入狱";
			tip = new BasicMessage(90, 25, string);
			tip.x = actionItem3.x + (actionItem3.width - tip.width) / 2;
			tip.y = actionItem3.y - tip.height;
			addChild(tip);
		}

		//保释
		private function onBailMouseOver(event:MouseEvent):void
		{
			var string:String = "保释出狱";
			tip = new BasicMessage(90, 25, string);
			tip.x = actionItem4.x + (actionItem4.width - tip.width) / 2;
			tip.y = actionItem4.y - tip.height;
			addChild(tip);
		}
		
		//驱逐
		private function onExpelMouseOver(event:MouseEvent):void
		{
			var string:String = "驱逐回家";
			tip = new BasicMessage(90, 25, string);
			tip.x = actionItem5.x + (actionItem5.width - tip.width) / 2;
			tip.y = actionItem5.y - tip.height;
			addChild(tip);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			if (null !=  tip && null != tip.parent)
        	{
        		tip.parent.removeChild(tip);
        	}
		}
		
		private function onRunAwayClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			RunAwayDialog.getInstance().open(animalInfo, SceneContainer.getInstance().currentScene);
		}

		private function onGoHomeClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			ServerHelper.getInstance().runAwayAnimal(animalInfo.id, animalInfo.ownerId, 0, onGoHomeComplete);
		}
		
		private function onGoHomeComplete(result:String):void
		{
			if (result == "success")
			{
				var home:Home = SceneContainer.getInstance().currentScene as Home;
				home.switchLiveId(animalInfo.ownerId);
			}
		}
		
		private function onCrimeClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			//加载战斗场面
			var home:Home = SceneContainer.getInstance().currentScene as Home;
			home.startFight(animalInfo.ownerId, [animalInfo.id]);
		}
		
		//指认通缉的在逃犯
		private function onArrestClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			var claimBones:int = Config.claimBonesForWrongIdentification;
			var detectivePlayer:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			ServerHelper.getInstance().identifyWantedAnimal(detectivePlayer.id, animalInfo.id, claimBones, onIdentifyComplete);
		}
		
		//保释
		private function onBailClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			var timeLeft:int = animalInfo.timeToGetFree - ServerHelper.getInstance().now();
			var bailBones:int = Math.max(timeLeft * Config.bonePerSecondsInPrison, 10);
			var home:Home = SceneContainer.getInstance().currentScene as Home;
			msgBox = new MessageBox("需要缴纳" + bailBones + "骨头才能重获自由。是否继续？", 1, true);
			msgBox.x = (BFont.stageWidth - msgBox.width) / 2;
			msgBox.y = (BFont.stageHeight - msgBox.height) / 2;
			msgBox.buttonOK.addEventListener(MouseEvent.CLICK, onBailOKClick);
			home.addChild(msgBox);
		}
				
		private function onBailOKClick(event:MouseEvent):void
		{
			var timeLeft:int = animalInfo.timeToGetFree - ServerHelper.getInstance().now();
			var bailBones:int = Math.max(timeLeft * Config.bonePerSecondsInPrison, 10);
			ServerHelper.getInstance().bailAnimal(animalInfo.id, bailBones, onBailOutComplete);
		}
		
		//驱逐
		private function onExpelClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			ServerHelper.getInstance().runAwayAnimal(animalInfo.id, animalInfo.ownerId, 0, onExpelComplete);
		}
		
		private function onExpelComplete(result:String):void
		{
			if (result == "success")
			{
				var home:Home = SceneContainer.getInstance().currentScene as Home;
				var homeAnimal:HomeAnimal = home.homeAnimals[animalInfo.id];
				if (homeAnimal.x < BFont.stageWidth / 2)
				{
					TweenLite.to(homeAnimal, 1, {x:200, y:-300, scaleX:0, scaleY:0, rotation: 90, onComplete:onAnimalFlyoutComplete, onCompleteParams:[homeAnimal]});
				}
				else
				{
					TweenLite.to(homeAnimal, 1, {x:-200, y:-300, scaleX:0, scaleY:0, rotation: -90, onComplete:onAnimalFlyoutComplete, onCompleteParams:[homeAnimal]});
				}
			}
		}
		
		private function onAnimalFlyoutComplete(homeAnimal:HomeAnimal):void
		{
			if (homeAnimal != null && homeAnimal.parent != null)
			{
				homeAnimal.parent.removeChild(homeAnimal);
				homeAnimal = null;
			}
		}
				
		private function onBailOutComplete(result:String, animalId:int):void
		{
			
			if (result == "success")
			{
				msgBox.close();
				var home:Home = SceneContainer.getInstance().currentScene as Home;
				home.startRemovePrisonMovie(animalId);
			}
			else if (result == "not enough bones")
			{ 
        		var messageText:String = "没有足够的骨头用于保释！";
	        	var warningBox:MessageBox = new MessageBox(messageText);
	        	warningBox.x = (BFont.stageWidth - warningBox.width) / 2;
	        	warningBox.y = (BFont.stageHeight - warningBox.height) / 2;
	        	SceneContainer.getInstance().currentScene.addChild(warningBox);
        	}
		}
		
		private function onIdentifyComplete(result:String, wantedBonesSum:int, battleCases:Array):void
		{
			
			var rejectFlag:Boolean;
			if (result == "right")
			{
				rejectFlag = false;
			}
			else
			{
				rejectFlag = true;
			}
			var claimBones:int = Config.claimBonesForWrongIdentification;
			var detectivePlayer:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			var detectiveAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[detectivePlayer.ownAnimals[0]];
			var arrestDialog:ArrestWarrantDialog = new ArrestWarrantDialog(detectiveAnimal, animalInfo, rejectFlag, claimBones, 1, null, wantedBonesSum, battleCases);
			arrestDialog.x = (BFont.stageWidth - arrestDialog.width) / 2;
			arrestDialog.y = 80;
			SceneContainer.getInstance().currentScene.addChild(arrestDialog);
		}
	}
}